/**
 * 
 */
package fr.esiag.isidis.fi.pdsTeam.Test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.apache.activemq.ActiveMQConnectionFactory;

import fr.esiag.isidis.fi.pdsTeam.recive.beans.*;
import fr.esiag.isidis.fi.pdsTeam.send.beans.*;




/**
 * @author Admin
 *
 */
public class CentreControleMock extends JFrame implements MessageListener,ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1369842773097474412L;
	@SuppressWarnings("rawtypes")
	private JList mListR;
	@SuppressWarnings("rawtypes")
	private DefaultListModel model;
	private JTextField text;
	private Session session;
	private MessageProducer producer;

	/**
	 * @throws JMSException 
	 * 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public CentreControleMock() throws JMSException {
		this.setSize(600, 400);
		this.setTitle("Centre du Controle");
		JPanel panel=new JPanel();
		panel.setLayout(new BorderLayout());
		JPanel receive=new JPanel();
		/*
		 Receive
		 */
		model = new DefaultListModel();
		mListR = new JList(model);
		JScrollPane pane = new JScrollPane(mListR);
		receive.add(pane);
		/*
		 Producer
		 */
		JPanel panelP=new JPanel();
		text = new JTextField(10);
		JButton button=new JButton("send Action");
		panelP.add(text);
		panelP.add(button);
		button.addActionListener(this);
		/**/
		panel.add(receive, BorderLayout.WEST);
		panel.add(panelP,BorderLayout.EAST);
		this.getContentPane().add(panel);
		// TODO Auto-generated constructor stub
		this.getContentPane().add(panel);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		ConnectionFactory factory = new ActiveMQConnectionFactory();
		Connection connection = factory.createConnection();
		connection.start();
		
		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		
		Destination destinationR = session.createQueue("QUEUE-PDSTeam");
		
		MessageConsumer consumer = session.createConsumer(destinationR);
		
		Destination destinationP = session.createQueue("QUEUE-CentreControle");
		
		producer = session.createProducer(destinationP);
		
		consumer.setMessageListener(this);
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			new CentreControleMock();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	
	public void onMessage(Message msg) {
		// TODO Auto-generated method stub
		 if(msg instanceof ObjectMessage){
			ObjectMessage ObjMsg = (ObjectMessage)msg;
			try {
				Information inf = (Information) ObjMsg.getObject();
				model.addElement(inf);
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}

	
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		Action A=new Action();
		A.setOrder(new Order(new IdSource(Integer.parseInt(text.getText())),new IdAction(1),new TypeSource(0)));
		//A.setOrder(text.getText());
		ObjectMessage msg;
		try {
			msg = session.createObjectMessage();
			msg.setObject(A);
			producer.send(msg);
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}
